<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="借阅人姓名" prop="borrowingName">
        <el-input
          v-model="dataForm.borrowingName"
          placeholder="借阅人姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <!-- <el-input
          v-model="dataForm.sex"
          placeholder="性别 1：男 2：女"
        ></el-input> -->
        <el-radio v-model.number="dataForm.sex" :label="1">男</el-radio>
        <el-radio v-model.number="dataForm.sex" :label="2">女</el-radio>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话号码"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        borrowingName: "",
        sex: 1,
        age: "",
        phone: ""
      },
      dataRule: {
        borrowingName: [
          { required: true, message: "借阅人姓名不能为空", trigger: "blur" }
        ],
        sex: [
          {
            required: true,
            message: "性别 1：男 2：女不能为空",
            trigger: "blur"
          }
        ],
        age: [{ required: true, message: "年龄不能为空", trigger: "blur" }],
        phone: [
          { required: true, message: "电话号码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/contract/borrowinguser/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.borrowingName = data.borrowingUser.borrowingName;
              this.dataForm.sex = data.borrowingUser.sex;
              // console.log(this.dataForm.sex);
              this.dataForm.age = data.borrowingUser.age;
              this.dataForm.phone = data.borrowingUser.phone;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/contract/borrowinguser/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              borrowingName: this.dataForm.borrowingName,
              sex: this.dataForm.sex,
              age: this.dataForm.age,
              phone: this.dataForm.phone
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
